import dayjs from 'dayjs'
// 新疆
let userIdAttendabceArr = [82] // 罗平
// 郑州
let deptAttendabce = [8253, 6707, 6708, 8254, 6701, 6702, 6703, 6704, 8255, 8256, 8242] // 8242郑州部门id
// 郑州郑州绿地之窗景峰座1号楼打卡
let cxyuserIdAttendabceArr = [147,106,108,99,163,97] // 郑州郑州绿地之窗景峰座1号楼打卡
export const getDate = () => {
  let now = new Date()
  let year = now.getFullYear()
  let month = now.getMonth() + 1
  let date = now.getDate()

  return year + '-' + month + '-' + date
}

export const getNowDate = (now) => {
  //将时间戳格式转换成年月日时分秒
  let date = new Date(now)
  let Y = date.getFullYear() + '-'
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '

  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  let strDate = Y + M + D
  // let strDate = Y + M + D + h + m + s;
  return strDate
}

// 获取地区的工作时间配置
const getWorkHoursConfig = (userId, dept) => {
  if (userIdAttendabceArr.includes(userId)) {
    // 新疆
    return {
      morningStart: { hour: 8, minute: 0 },
      morningEnd: { hour: 12, minute: 30 },
      afternoonStart: { hour: 14, minute: 30 },
      afternoonEnd: { hour: 19, minute: 0 },
      dailyTotal: 9, // 新疆的总工作时长
    }
  } else if (deptAttendabce.includes(dept)) {
    // 郑州
    return {
      morningStart: { hour: 8, minute: 30 },
      morningEnd: { hour: 12, minute: 0 },
      afternoonStart: { hour: 14, minute: 0 },
      afternoonEnd: { hour: 18, minute: 30 },
      dailyTotal: 8, // 郑州的总工作时长
    }
  }else if(cxyuserIdAttendabceArr.includes(userId)){
    // 郑州郑州绿地之窗景峰座1号楼打卡
    return {
      morningStart: { hour: 9, minute: 0 },
      morningEnd: { hour: 12, minute: 0 },
      afternoonStart: { hour: 14, minute: 0 },
      afternoonEnd: { hour: 19, minute: 0 },
      dailyTotal: 8, // 郑州郑州绿地之窗景峰座1号楼打卡
    }
  } else {
    // 默认
    return {
      morningStart: { hour: 8, minute: 0 },
      morningEnd: { hour: 12, minute: 0 },
      afternoonStart: { hour: 14, minute: 30 },
      afternoonEnd: { hour: 18, minute: 30 },
      dailyTotal: 8, // 默认的总工作时长
    }
  }
}

// 计算某一天的上班时长，支持0.5小时精度
const calculateWorkHoursForDay = (leaveStart, leaveEnd, date, userId, dept) => {
  const config = getWorkHoursConfig(userId, dept)

  const workStartMorningDate = dayjs(date)
    .set('hour', config.morningStart.hour)
    .set('minute', config.morningStart.minute)
  const workEndMorningDate = dayjs(date)
    .set('hour', config.morningEnd.hour)
    .set('minute', config.morningEnd.minute)
  const workStartAfternoonDate = dayjs(date)
    .set('hour', config.afternoonStart.hour)
    .set('minute', config.afternoonStart.minute)
  const workEndAfternoonDate = dayjs(date)
    .set('hour', config.afternoonEnd.hour)
    .set('minute', config.afternoonEnd.minute)

  // 如果请假时间完全不在工作时间内，则返回 0
  if (leaveEnd.isBefore(workStartMorningDate) || leaveStart.isAfter(workEndAfternoonDate)) {
    return 0
  }

  let workHours = 0

  // 计算早上工作时间
  if (leaveStart.isBefore(workEndMorningDate) && leaveEnd.isAfter(workStartMorningDate)) {
    const workStart = leaveStart.isBefore(workStartMorningDate) ? workStartMorningDate : leaveStart
    const workEnd = leaveEnd.isBefore(workEndMorningDate) ? leaveEnd : workEndMorningDate
    const hoursDiff = workEnd.diff(workStart, 'hour', true)
    workHours += roundToHalfHour(hoursDiff)
  }

  // 计算下午工作时间
  if (leaveStart.isBefore(workEndAfternoonDate) && leaveEnd.isAfter(workStartAfternoonDate)) {
    const workStart = leaveStart.isBefore(workStartAfternoonDate)
      ? workStartAfternoonDate
      : leaveStart
    const workEnd = leaveEnd.isBefore(workEndAfternoonDate) ? leaveEnd : workEndAfternoonDate
    const hoursDiff = workEnd.diff(workStart, 'hour', true)
    workHours += roundToHalfHour(hoursDiff)
  }

  return workHours
}

// 辅助函数：将小时数四舍五入到最近的0.5小时
const roundToHalfHour = (hours) => {
  const rounded = Math.round(hours * 2) / 2
  return rounded < 0.5 ? 0.5 : rounded // 确保最小为0.5小时
}

// 计算跨多个工作日的总上班时长，支持0.5小时精度
export const calculateTotalWorkHours = (leaveStartTime, leaveEndTime, userId, dept) => {
  let totalWorkHours = 0

  let leaveStart = dayjs(leaveStartTime)
  let leaveEnd = dayjs(leaveEndTime)

  // 获取地区配置
  const config = getWorkHoursConfig(userId, dept)

  // 如果请假时间是同一天
  if (leaveStart.isSame(leaveEnd, 'day')) {
    totalWorkHours += calculateWorkHoursForDay(leaveStart, leaveEnd, leaveStart, userId, dept)
  } else {
    // 处理跨天的情况
    // 计算从请假开始时间到该天结束的工作时间
    totalWorkHours += calculateWorkHoursForDay(
      leaveStart,
      dayjs(leaveStart).endOf('day'),
      leaveStart,
      userId,
      dept,
    )

    // 计算中间所有完整工作日的工作时间
    let currentDay = leaveStart.add(1, 'day').startOf('day')
    while (currentDay.isBefore(leaveEnd, 'day')) {
      totalWorkHours += config.dailyTotal // 使用配置中的每日总工作时长
      currentDay = currentDay.add(1, 'day')
    }

    // 计算从第二天开始直到请假结束时间的工作时间
    totalWorkHours += calculateWorkHoursForDay(
      dayjs(leaveEnd).startOf('day'),
      leaveEnd,
      leaveEnd,
      userId,
      dept,
    )
  }

  // 确保最终结果也是0.5小时的倍数
  return roundToHalfHour(totalWorkHours)
}
